package cn.yinzhiping.febsauth.project.manager;

import cn.yinzhiping.febs.common.entity.system.Menu;
import cn.yinzhiping.febs.common.entity.system.SystemUser;
import cn.yinzhiping.febsauth.project.mapper.MenuMapper;
import cn.yinzhiping.febsauth.project.mapper.UserMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用于统一定义和用户相关的业务方法：
 */
@Service
@SuppressWarnings("all")
public class UserManager {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MenuMapper menuMapper;

    public SystemUser findByName(String username) {
        return userMapper.findByName(username);
    }

    public String findUserPermissions(String username) {
        List<Menu> userPermissions = menuMapper.findUserPermissions(username);

//        List<String> perms = new ArrayList<>();
//        for (Menu m: userPermissions){
//            perms.add(m.getPerms());
//        }
//        return StringUtils.join(perms, ",");
        //通过java的stream简化
          return userPermissions.stream().map(Menu::getPerms).collect(Collectors.joining(","));
    }
}

